const express = require('express');
const router = express.Router();
const wish = require("../controllers/wishController");
const wishObj = new wish();
const SensitiveFilter = require('sensitive-word-filter')
// 编写接口
// 发布祝福
// 请求方式 post
// 请求地址：http://localhost:3001/api/wish/publish
// 参数： wish_text, imgUrl, user_id, username 
router.post('/publish', async (req, res) => {
    let { wish_text, imgUrl, user_id, username } = req.body;

    if (wish_text == undefined || imgUrl == undefined || user_id == undefined || username == undefined) {
        return res.send({
            code: -1,
            msg: "缺少参数"
        })
    }
    const filteredWishText = SensitiveFilter.filter(wish_text);
    if (filteredWishText != wish_text) {
        res.send({
            code: -1,
            msg: "请勿发布敏感信息"
        })
    } else {
        await wishObj.publish(filteredWishText, imgUrl, user_id, username)
            .then(
                resule => {
                    res.send(resule)
                },
                err => {
                    res.send(err)
                }
            )
    }


})

// 修改祝福
// 请求方式 post
// 请求地址：http://localhost:3001/api/wish/modify?wish_text=测试&id=1
// 参数： wish_text id
router.post('/modify', async (req, res) => {
    let { wish_text, id } = req.body;
    if (wish_text == undefined || id == undefined) {
        res.send({
            code: -1,
            msg: "缺少参数"
        })
    } else {
        await wishObj.modify(wish_text, id).then(
            result => {
                res.send(result)
            },
            err => {
                res.send(err)
            }
        )
    }
})

// 删除祝福
// 请求方式 post
// 请求地址：http://localhost:3001/api/wish/remove?&id=1
// 参数：  id
router.post('/remove', async (req, res) => {
    let id = req.body.id;
    if (id == undefined) {
        res.send({
            code: -1,
            msg: '缺少参数'
        })
    } else {
        await wishObj.remove(id).then(
            result => {
                res.send(result)
            },
            err => {
                res.send(err)
            }
        )
    }
})

// 获取祝福列表
// 请求方式 post
// 请求地址：http://localhost:3001/api/wish/list
// 参数：page_num page_size
router.post('/list', async (req, res) => {
    const { page_num, page_size } = req.body;
    if (page_num == undefined || page_size == undefined) {
        res.send({
            code: -1,
            msg: '缺少参数'
        })
    }
    await wishObj.list(page_num, page_size).then(
        result => {
            res.send(result)
        },
        err => {
            res.send(err)
        }
    )
})


// 搜索祝福
// 请求方式 post
// 请求地址：http://localhost:3001/api/wish/search?keyword=测试
// 参数：keyword
router.post('/search', async (req, res) => {
    let keyword = req.body.keyword;
    if (keyword == undefined) {
        res.send({
            code: -1,
            msg: '缺少参数'
        })
    }
    await wishObj.search(keyword).then(
        result => {
            res.send(result)
        },
        err => {
            res.send(err)
        }
    )
})

module.exports = router